from django.db import models

# Create your models here.
class coffee(models.Model):
    id = models.AutoField(primary_key=True)
    title = models.CharField(max_length=120)
    category_id = models.IntegerField(default=0)
    category = models.CharField(max_length=200,null=True)
    cover = models.CharField(max_length=200)
    gallery = models.TextField()
    user_id = models.IntegerField(default=0)
    description = models.CharField(max_length=120)
    content = models.TextField()
    template = models.IntegerField(default=0)
    custom_fields = models.TextField()
    create_time = models.IntegerField(default=0)
    update_time = models.IntegerField(default=0)
    recommend = models.IntegerField(default=0)
    status = models.IntegerField(default=1)
    views = models.IntegerField(default=0)
    likes = models.IntegerField(default=0)

    class Meta:
        db_table = 'coffee'

class category(models.Model):
    id = models.AutoField(primary_key=True)
    name = models.CharField(max_length=20)
    pid = models.IntegerField(default=0)
    status = models.IntegerField(default=1)

    def get_all_children_ids(pk):
        def _get_children_ids(current_pk, result=None):
            if result is None:
                result = []

            children = category.objects.filter(pid=current_pk).values_list('id', flat=True)
            for child_id in children:
                result.append(child_id)
                _get_children_ids(child_id, result)
            return result
        return _get_children_ids(pk)

class fields(models.Model):
    id = models.AutoField(primary_key=True)
    name = models.CharField(max_length=20)
    field_type = models.CharField(max_length=50)
    value = models.CharField(max_length=50)
    required = models.IntegerField(default=0)
    min = models.IntegerField(default=0)
    max = models.IntegerField(default=200)
    placeholder = models.CharField(max_length=50,default='',null=True)
    show_count = models.IntegerField(default=0)
    status = models.IntegerField(default=1)

class templates(models.Model):
    id = models.AutoField(primary_key=True)
    name = models.CharField(max_length=20)
    fields = models.TextField()
    description = models.CharField(max_length=120,default='',null=True)
    create_time = models.IntegerField(default=0)
    status = models.IntegerField(default=1)